package cn.yangliu.nacos.app.web;

import javax.annotation.PostConstruct;

import cn.yangliu.nacos.app.feign.ProviderFeign;
import cn.yangliu.nacos.comm.model.Oauth2InsertModel;
import cn.yangliu.nacos.comm.model.Oauth2Token;
import cn.yangliu.nacos.comm.model.Oauth2UpdateOtherModel;
import cn.yangliu.nacos.comm.model.Oauth2UpdateOtherPasswordModel;
import cn.yangliu.nacos.comm.model.Oauth2UpdatePasswordModel;
import cn.yangliu.nacos.comm.model.Oauth2UpdateSelfModel;
import cn.yangliu.nacos.comm.model.Oauth2UserModel;
import cn.yangliu.nacos.comm.oauth.Oauth2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * The interface Test controller.
 *
 * @author 问道于盲
 * @date 2019 -12-08
 */
@RestController
@Api
public class TestController {

    /**
     * current class instance's member.
     * The Provider feign.
     */
    @Autowired
    private ProviderFeign providerFeign;

    /**
     * current class instance's member.
     * The Oauth 2 service.
     */
    @Autowired
    private Oauth2Service oauth2Service;

    /**
     * current class instance's member.
     * The Token.
     */
    private Oauth2Token token;

    /**
     * Init.
     */
    @PostConstruct
    public void init() {
        token = oauth2Service.login("admin", "admin");
    }

    /**
     * Test object.
     *
     * @return the object
     */
    @GetMapping("/")
    @ApiOperation("test")
    public Object test() {
        return providerFeign.test();
    }

    /**
     * Insert oauth 2 user model.
     *
     * @param model the model
     * @return the oauth 2 user model
     */
    @ApiOperation("insert")
    @PostMapping("/user/insert")
    public Oauth2UserModel insert(@RequestBody Oauth2InsertModel model) {
        Oauth2UserModel oauth2UserModel = oauth2Service.insert(model, token.getAccessToken());
        return oauth2UserModel;
    }

    /**
     * Update self oauth 2 user model.
     *
     * @param model the model
     * @return the oauth 2 user model
     */
    @ApiOperation("update")
    @PutMapping("/user/updateSelf")
    public Oauth2UserModel updateSelf(@RequestBody Oauth2UpdateSelfModel model) {
        return oauth2Service.updateSelf(model, token.getAccessToken());
    }

    /**
     * Update oauth 2 user model.
     *
     * @param model the model
     * @return the oauth 2 user model
     */
    @ApiOperation("updateOther")
    @PutMapping("/user/updateOther")
    public Oauth2UserModel update(@RequestBody Oauth2UpdateOtherModel model) {
        return oauth2Service.updateOther(model, token.getAccessToken());
    }

    /**
     * Update boolean.
     *
     * @param model the model
     * @return the boolean
     */
    @ApiOperation("updateSelfPassword")
    @PutMapping("/user/updateSelfPassword")
    public boolean update(@RequestBody Oauth2UpdatePasswordModel model) {
        return oauth2Service.updateSelfPassword(model.getPassword(), token.getAccessToken());
    }

    /**
     * Update boolean.
     *
     * @param model the model
     * @return the boolean
     */
    @ApiOperation("updateOtherPassword")
    @PutMapping("/user/updateOtherPassword")
    public boolean update(@RequestBody Oauth2UpdateOtherPasswordModel model) {
        return oauth2Service.updateOtherPassword(model, token.getAccessToken());
    }

    /**
     * Delete boolean.
     *
     * @param id the id
     * @return the boolean
     */
    @ApiOperation("delete")
    @DeleteMapping("/user/delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return oauth2Service.delete(id, token.getAccessToken());
    }

    /**
     * Principal oauth 2 user model.
     *
     * @return the oauth 2 user model
     */
    @ApiOperation("principal")
    @GetMapping("/user/principal")
    public Oauth2UserModel principal() {
        return oauth2Service.principal(token.getAccessToken());
    }
}
